// index.js
// 获取应用实例
const app = getApp()
const api = app.globalData.api

Page({
    data: {
        companyOption: [],
        coid: '',
        quickData: [],
        result: []
    },
    async main() {
        await this.getBacklog()
        await this.dataBoard()
        await this.menu()
        await this.getCompany()
    },
    async dataBoard() {
        let res = await api.curlGet('?cla=inventoryOther&fun=dataBoard')
        if (res.code === 1) {
            this.setData({
                result: res.data
            })
        } else {
            api.showToast(res.warn)
        }
    },
    // 获取菜单
    async menu() {
        let res = await api.curlGet('?cla=inventoryOther&fun=getDefaultFunc')
        if (res.code === 1) {
          this.setData({
            quickData: res.data
          })
          wx.setStorageSync('func', res.data)
        } else {
            api.showToast(res.warn)
        }
    },
    // 获取待办
    async getBacklog() {
        let res = await api.curlGet('?cla=inventoryOther&fun=backlog')
        if (res.code === 1) {
            this.setData({
                backlog: res.data
            })
        } else {
            api.showToast(res.warn)
        }
    },
    // 获取账套
    async getCompany() {
        let company = wx.getStorageSync('company'),
            coid = wx.getStorageSync('coid')
        if (!company) {
            let res = await api.curlGet('?cla=inventoryOther&fun=company')
            if (res.code === 1) {
                company = res.data.option
                coid = res.data.now
                wx.setStorageSync('company', company)
                wx.setStorageSync('coid', coid)
            } else {
                api.showToast(res.warn)
            }
        }
        this.setData({
            coid: coid,
            companyOption: company
        })
    },
    async onSwitchChange(e) {
        let coid = e.detail
        if (coid) {
            let res = await api.curlPost('?cla=inventoryOther&fun=setCompany', {coid: coid})
            if (res.code === 1) {
                this.setData({coid: coid})
                wx.setStorageSync('coid', coid)
            }
        }
    },
    allFeatures() {
        wx.switchTab({
            url: '/pages/function/index'
        })
    },
    setFunc() {
        wx.navigateTo({
            url: '/pages/setting/func'
        })
    },
    async setDefaultFunc() {
        const func = wx.getStorageSync('func')
        if (!func) {
          console.log('rr'+app.root)
            let res = await api.curlGet('?cla=inventoryOther&fun=getDefaultFunc')
            if (res.code === 1) {
                const func = res.data === '{}' ? {} :res.data
                wx.setStorageSync('func', func)
            }
        }
    },
    onLoad: function (options) {
      // 存储后台微信扫码登录参数
      if(options.scene){
        wx.setStorageSync('qrcodelogin', options.scene)
      }
    },
    onShow: async function (options) {
      // 触发后台扫码登录
      let qrcodelogin = wx.getStorageSync('qrcodelogin')
      let openid = wx.getStorageSync('openid')
      if(qrcodelogin && openid){
        await this.qrcodeLogin(qrcodelogin, openid)
      }
      await this.main()
    },
    // 触发后台扫码登录
    async qrcodeLogin(id, openid) {
      let res = await api.curlPost('?cla=loginApp&fun=qrcodeLogin', {id: id, openid: openid})
      if (res.code === 1 && res.data.warn === 2) {
        wx.setStorageSync('qrcodelogin', '')
      }
    },
    /// 长按清理信息
    clearStorage: function (act) {
      app.clearStorage()
    },
})
